package com.ihelp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ihelp.pojo.Request;
import com.ihelp.pojo.RequestDTO;
import com.ihelp.pojo.User;
import com.ihelp.mapper.UserMapper;
import com.ihelp.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ihelp.util.MyWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ihelp.util.JwtUtil;

import java.util.HashMap;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author LiuZiMing
 * @since 2023-05-23
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;


    public List<User> getAllUser() {
        MPJLambdaWrapper<User> wrapper = JoinWrappers.lambda(User.class)
                .selectAll(User.class);   //查询user表全部字段
        List<User> user = userMapper.selectList(wrapper);
        return user;
    }

    public User getUserById(Integer id) {
        User user = userMapper.selectById(id);
        return user;
    }

    @Override
    public Integer createUser(String nickname, String avatarFileName, String username, String password, String area, Long phoneNumber, String birthday, String personalIntroduction) {
        User user = new User();
        user.setNickname(nickname);
        user.setAvatarFileName(avatarFileName);
        user.setUsername(username);
        user.setPassword(password);
        user.setArea(area);
        user.setPhoneNumber(phoneNumber);
        user.setBirthday(birthday);
        user.setPersonalIntroduction(personalIntroduction);
        int result = userMapper.insert(user);
        return result;
    }

    @Override
    public User getUserByUsernameAndPassword(String username, String password) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, username)
                .eq(User::getPassword, password);
        return userMapper.selectOne(wrapper);
    }

    @Override
    public List<User> getUserByDynamicConditions(Integer id, String nickname, String avatarFileName, String username, String password,
                                                 String area, Long phoneNumber, String birthday, String personalIntroduction,
                                                 Double credit, Double rate, Double accompanyTime, Double responseTime, Integer accompanyNumber) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();

        for (int i = 0; i < 1; i++) {
            if (id != null && id > 0) {
                userQueryWrapper.eq("id", id);
            }
            if (nickname != null) {
                userQueryWrapper.eq("nickname", nickname);
            }
            if (avatarFileName != null) {
                userQueryWrapper.eq("avatar_file_name", avatarFileName);
            }
            if (username != null) {
                userQueryWrapper.eq("username", username);
            }
            if (password != null) {
                userQueryWrapper.eq("password", password);
            }
            if (area != null) {
                userQueryWrapper.eq("area", area);
            }
            if (phoneNumber != null && phoneNumber > 0) {
                userQueryWrapper.eq("phone_number", phoneNumber);
            }
            if (birthday != null) {
                userQueryWrapper.eq("birthday", birthday);
            }
            if (personalIntroduction != null) {
                userQueryWrapper.eq("personal_introduction", personalIntroduction);
            }
            if (credit != null) {
                userQueryWrapper.eq("credit", credit);
            }
            if (rate != null) {
                userQueryWrapper.eq("rate", rate);
            }
            if (accompanyTime != null) {
                userQueryWrapper.eq("accompany_time", accompanyTime);
            }
            if (responseTime != null) {
                userQueryWrapper.eq("response_time", responseTime);
            }
            if (accompanyNumber != null) {
                userQueryWrapper.eq("accompany_number", accompanyNumber);
            }
        }

        List<User> userList = userMapper.selectList(userQueryWrapper);
        return userList;
    }


}
